﻿using System;
using System.IO;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using Maticsoft.Model;
using Maticsoft.BLL;

namespace FineUI.Examples
{
    public partial class student_resubmit_document : System.Web.UI.Page
    {

        protected DataSet GetCourseInfrom(int index)
        {
            string currentS_Id = Session["UserName"].ToString();
            Maticsoft.BLL.SelectCourse SC = new Maticsoft.BLL.SelectCourse();
            DataSet DSCourse = SC.GetCourse("SelectCourse.TC_Id=TeachingClass.TC_Id and TeachingClass.T_Id=Teacher.T_Id and SelectCourse.S_Id= '" + currentS_Id + "' order by SelectCourse.TC_Id");


            DataView currentView = DSCourse.Tables[0].DefaultView;
            DropDownListCourseName.DataTextField = "TC_Name";
            DropDownListCourseName.DataValueField = "TC_Id";
            DropDownListCourseName.DataSource = currentView;
            DropDownListCourseName.DataBind();

            string currentT_Name = DSCourse.Tables[0].Rows[index]["T_Name"].ToString();
            Session["CurrentT_Id"] = DSCourse.Tables[0].Rows[index]["T_Id"].ToString();
            labTeacherName.Text = currentT_Name;


            return DSCourse;

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null)
            {
                Response.Redirect("~/login.aspx");
                return;
            }


            if (!IsPostBack)
            {
                GetCourseInfrom(0);
                labelTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
            }

        }

        protected void btnCreateDoc_Click(object sender, EventArgs e)
        {
            //正则表达式
            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"^.+\.(doc)|(docx)$");
            if (!regex.IsMatch(fileUploadDoc.FileName))
            {
                Alert.Show("不支持该文件格式", MessageBoxIcon.Information);

            }
            else
            {
                Maticsoft.BLL.ReportSubmit docSubmit = new Maticsoft.BLL.ReportSubmit();
                Maticsoft.Model.ReportSubmit docSubmitInfrom = new Maticsoft.Model.ReportSubmit();

                TimeSpan document_time = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
                fileUploadDoc.SaveAs(Server.MapPath(@"~/document/student/") + "\\" + Convert.ToInt64(document_time.TotalSeconds).ToString() + fileUploadDoc.ShortFileName.ToString());

                Guid guidDoc = Guid.NewGuid();
                string currentT_Id = Session["CurrentT_Id"].ToString();
                string currentS_Id = Session["UserName"].ToString();

                docSubmitInfrom.RS_Id = guidDoc.ToString();
                docSubmitInfrom.T_Id = currentT_Id;
                docSubmitInfrom.S_Id = currentS_Id;
                docSubmitInfrom.RS_Title = tbxReportTitle.Text.ToString();
                docSubmitInfrom.RS_Formerly = fileUploadDoc.ShortFileName.ToString();
                docSubmitInfrom.RS_Path = Convert.ToInt64(document_time.TotalSeconds).ToString() + fileUploadDoc.ShortFileName.ToString();
                docSubmitInfrom.RS_Correcting = DropDownListCourseName.SelectedText.ToString();
                docSubmitInfrom.RS_Socre = "未评分";
                docSubmitInfrom.RS_Ask = txareaQuestion.Text.ToString();
                docSubmitInfrom.RS_Ans = "暂未回复";
                docSubmitInfrom.RS_Share = "未共享";
                docSubmitInfrom.RS_Date = labelTime.Text.ToString();

                if (docSubmit.Add(docSubmitInfrom))
                {
                    Alert.Show("报告上传成功", MessageBoxIcon.Information);
                    Response.Redirect("~/student_manage_document.aspx");
                }

            }




        }

        protected void DropDownListCourseName_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = DropDownListCourseName.SelectedIndex;
            GetCourseInfrom(index);
            DropDownListCourseName.SelectedIndex = index;
            
        }





    }
}